package com.hucheng.rest.web;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.hucheng.common.exception.EduException;
import com.hucheng.common.response.ServiceCoreVO;
import com.hucheng.pay.service.AlipayService;
import com.hucheng.rest.base.BaseAction;
import org.springframework.stereotype.Controller;

import java.io.UnsupportedEncodingException;

/**
 * @Author: 凉白开不加冰
 * @Version: 0.0.1V
 * @Date: 2019-07-16
 * @Description: 支付宝支付
 **/
@Controller("alipayAction")
public class AlipayAction extends BaseAction {

    @Reference
    private AlipayService alipayService;

    /**
     * 订单未支付，网页支付
     *
     * @return
     * @throws EduException
     */
    public ServiceCoreVO pagePayV100() throws EduException, AlipayApiException, UnsupportedEncodingException {

        String orderId = getRequestContentValue("order_id");
        String orderNumber = getRequestContentValue("order_number");
        String payAmount = getRequestContentValue("pay_amount");

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("form", alipayService.alipayTradePagePay(userId, orderId, orderNumber, payAmount));

        return response(jsonObject);
    }

    /**
     * 订单未支付，扫码当面付，预创建支付订单
     *
     * @return
     * @throws EduException
     */
    public ServiceCoreVO precreateV100() throws EduException, AlipayApiException, UnsupportedEncodingException {

        String orderId = getRequestContentValue("order_id");
        String orderNumber = getRequestContentValue("order_number");
        String payAmount = getRequestContentValue("pay_amount");

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("qr_code", alipayService.precreate(userId, orderId, orderNumber, payAmount));

        return response(jsonObject);
    }

}
